<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html >
<html lang="en">
<head>
<%@include file="../../../common/includeBaseSet.jsp" %>
<%@include file="../../../common/includeSystemSet.jsp" %>
<%@include file="../../../common/operation_forbid.jsp" %>
<%@include file="../../../common/jsVersion.jsp" %>
<style type="text/css">
	a{cursor:pointer}
</style>
</head>
<body onload="loadQuestionnaireModel()">
    <div class="page-content">
        <form id="baseForm" class="form-horizontal">
            <input type='hidden' class='pageNum' name='pageNum' value='1'/>
            <input type='hidden' class='pageSize' name='pageSize' value='10'/>
	        <div class="col-xs-12 panel panel-default">
	        	<div style="margin-top:1%">
		            <div class="col-xs-6">
		                <div class="form-group">
		                    <label class="col-xs-3 control-label">问卷搜索:</label>
		                    <div class="col-xs-6">
		                        <input class="form-control" type="text" name="name" id="name" onkeydown="return ClearSubmit(event)"
		                        placeholder="可根据问卷名称或创建人查询" onfocus="this.placeholder=''" onblur="this.placeholder='可根据问卷名称或创建人查询'"/>
		                    </div>
		                </div>
		            </div>
		            <div class="col-xs-4">
		                <div class="form-group">
		                    <div>
		                        <button id='searchBtn' class="btn btn-warning  btn-xs"  style="width:70px;"
		                            title="查询" type="button" onclick="JY.Page.jump('baseForm',1,'loadQuestionnaireModel')">
		                            <i class="icon-search bigger-110 icon-only">搜索</i>
		                        </button>
       							<c:choose>
       								<c:when test="${add == 'add'}">
		                        <button id='searchBtn' class="btn btn-warning  btn-xs"  style="width:100px;margin-left:10px"
		                            title="新增模板" type="button" onclick="addQuestionnaireModel()">
		                            <i class="bigger-110 icon-only">新增模板</i>
		                        </button>
	                        		</c:when>
	                        	</c:choose>
		                    </div>
		                </div>
		            </div>
	            </div>
	        </div>
	    </form>
        <div class="col-xs-12">
            <table id="baseTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th class="center">编号</th>
                        <th class="center" style="width:400px">操作</th>
                        <th class="center">问卷名称</th>
                        <th class="center">创建时间</th>
                        <th class="center">创建人</th>
                        <th class="center">启用状态</th>
                    </tr>
                </thead>
                <tbody><!-- 内容 --></tbody>
            </table>
            <div class="row">
                <div class="col-sm-8">
                    <!--设置分页位置-->
                    <div id="pageing" class="dataTables_paginate paging_bootstrap">
                        <ul class="pagination"></ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- #dialog-confirm -->
        <%@include file="../../common/dialog.jsp"%>
    </div>
</body>
<script type="text/javascript">
	
	//按钮权限
	var btnEdit = "${edit}";
	var btnDelete = "${delete}";
	var btnExport = "${export}";
	var btnCopy = "${copy}";
	var btnValid = "${valid}";
	//查询列表
	function loadQuestionnaireModel(){
		var loadIndex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
		JY.Ajax.doRequest("baseForm",jypath +'/customer/questionnaireModel/list',null,function(data){
            $("#baseTable tbody").empty();
            var page = data.obj;
            //分页数据
            var pageNum = page.pageNumber, pageSize = page.pageSize, totalRecord = page.total;
            var html = "";
            var results = page.rows;
            if(results == null || results.length <= 0){
            	html+="<tr><td colspan='6' class='center'>没有相关数据</td></tr>";
                $("#baseTable tbody").append(html);
                $("#pageing ul").empty();//清空分页
                layer.closeAll('loading');
                return false;
            }
          
            var leng=(pageNum-1)*pageSize;//计算序号
            for(var i = 0;i<results.length;i++){
                var info = results[i];
                var toEditUrl = jypath +"/customer/questionnaireModel/toQuestionEdit?modelId=" + info.id;
                var toViewUrl = jypath +"/customer/questionnaireModel/toView?modelId=" + info.id;
                var exportToWord = jypath +"/customer/questionnaireModel/exportToWord?id=" + info.id;

                var valid = "不启用" //列表展示
                var isValidName = "启用" //操作按钮
                var isValid = "1";
                if(info.isValid == "1"){
                	valid = "启用";
                	isValidName = "不启用"
                	isValid = "0";
                }
                html += "<tr>";
                html += "<td class='center'>" + (leng + i + 1) + "</td>";
                html += "<td class='center'>";
                html += "<a href='" + toViewUrl + "'>预览</a>";
                if(btnCopy == "copy"){
                	html += "&nbsp;&nbsp;&nbsp;<a onclick='copyQuestionnaireModel(" + info.id + ")'>复制</a>";
                }
                if(btnExport == "export"){
                	html += "&nbsp;&nbsp;&nbsp;<a href='"+exportToWord+"'>导出word</a>";
                }
                if(btnValid == "valid"){
                	html += "&nbsp;&nbsp;&nbsp;<a onclick='validProjectQuestionnaire(" + info.id + ", " + isValid + ", \"" + info.name + "\")'>" + isValidName + "</a>";
                }
				if(btnDelete == "delete"){
					html += "&nbsp;&nbsp;&nbsp;<a onclick='deleteQuestionnaireModel(" + info.id + ", \"" + info.name + "\")'>删除</a>";
                }
                html += "</td>";
                html += "<td class='center'>";
                if(btnEdit == "edit"){
                	html += "<a href='" + toEditUrl + "' >" + info.name + "</a>";
                }else{
                	html += info.name;
                }
                if(info.questionnaireDescribe != ""){
                	html += "&nbsp;&nbsp;&nbsp;<a title='" + info.questionnaireDescribe + "'>问卷描述</a>";
                }
                html += "</td>";
                html += "<td class='center'>" + JY.Date.Format(info.createTime, 'yyyy-MM-dd') + "</td>";
                html += "<td class='center'>" + info.createUserName + "</td>";
                html += "<td class='center'>" + valid + "</td>";
                html += "</tr>";
            }
            $("#baseTable tbody").append(html);
            JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"loadQuestionnaireModel");
            layer.closeAll('loading');
		});
	}
	
	//弹出新增模板框
	function addQuestionnaireModel(){
		layer.open({
			type:2,
		    title: "新增模板",
		    closeBtn: 1,
		    shadeClose: true,
		    //maxmin: true, //开启最大化最小化按钮
		    area: ['560px', '450px'],
		    offset: ['50px', '350px'],
		    fix: false, //不固定
		    content: jypath +'/customer/questionnaireModel/toAdd'
		});
	}
	
	//弹出复制模板框
	function copyQuestionnaireModel(id){
		layer.open({
			type:2,
		    title: "复制模板",
		    closeBtn: 1,
		    shadeClose: true,
		    //maxmin: true, //开启最大化最小化按钮
		    area: ['560px', '400px'],
		    offset: ['50px', '300px'],
		    fix: false, //不固定
		    content: jypath +'/customer/questionnaireModel/toCopy?id='+id
		});
	}
	
	//删除问卷模板
	function deleteQuestionnaireModel(modelId, modelName){
		var msg = "是否删除模板《" + modelName + "》，删除后不能恢复模板";
		//询问框
		layer.confirm(msg, {
			offset: ['150px', '300px'],
		  	btn: ['是','否'] //按钮
		}, function(){
			layer.closeAll('dialog');
			deleteModel(modelId);
		});
	}
	function deleteModel(modelId){
		if(modelId == undefined || modelId == null 
				|| modelId == ""){
			layer.msg("删除问卷失败，请刷新页面后重试");
			return false;
		}
		$.ajax({
			type: 'post',
            url: '${jypath}/customer/questionnaireModel/edit',
            data:{
                "id": modelId,
                "delFlag": "1"
            },
            dataType: 'json',
            success: function(data){
            	if(data.res == "1"){
            		layer.msg("问卷已删除");
            		$('input[name="pageNum"]').val("1");
            		loadQuestionnaireModel();
            	}else{
            		layer.msg(data.resMsg);
            	}
            }
		});
	}
	
	//根据按钮设置是否启用
	function validProjectQuestionnaire(id, valid, questionnaireName){
		var validName = "不启用";
		if(valid == "1"){
			validName = "启用";
		}
		var msg = "是否将问卷《" + questionnaireName + "》设置为" + validName;
		//询问框
		layer.confirm(msg, {
			offset: ['150px', '350px'],
		  	btn: ['是','否'] //按钮
		}, function(){
			layer.closeAll('dialog');
			isValid(id, valid, validName);
		});
	}
	function isValid(id, valid, validName){
		console.log(validName);
		$.ajax({
			type: 'post',
            url: '${jypath}/customer/questionnaireModel/edit',
            data:{
                "id": id,
                "isValid": valid
            },
            dataType: 'json',
            success: function(data){
            	if(data.res == "1"){
            		layer.msg("问卷已设置为" + validName);
            		loadQuestionnaireModel();
            	}else{
            		layer.msg(data.resMsg);
            	}
            }
		});
	}
	
	//解决选中input框按回车时会刷新页面的问题
	function ClearSubmit(e) {
        if (e.keyCode == 13) {
            return false;
        }
	}
</script>
</html>